﻿namespace QMVnSoft.Controls.ChildGridControls
{
    using QMVnSoft.Controls;
    using System;
    using System.Windows.Forms;

    public class OptionEditing : TextOption, IDataGridViewEditingControl
    {
        private DataGridView dataGridView;
        private int ii_TrueTextLength = -1;
        private int rowIndex;
        private bool valueChanged;

        public virtual void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.BackColor = dataGridViewCellStyle.BackColor;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
        }

        public virtual bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            switch ((keyData & Keys.KeyCode))
            {
                case Keys.Space:
                    base.Value = base.SearchString();
                    this.ii_TrueTextLength = this.Text.Length;
                    return false;

                case Keys.End:
                    return true;

                case Keys.Home:
                    return true;

                case Keys.Left:
                {
                    TextOption option2 = this;
                    if ((option2 == null) || (((this.RightToLeft != RightToLeft.No) || ((option2.SelectionLength == 0) && (option2.SelectionStart == 0))) && ((this.RightToLeft != RightToLeft.Yes) || ((option2.SelectionLength == 0) && (option2.SelectionStart == option2.Text.Length)))))
                    {
                        break;
                    }
                    return true;
                }
                case Keys.Up:
                    return true;

                case Keys.Right:
                {
                    TextOption option = this;
                    if ((option == null) || (((this.RightToLeft != RightToLeft.No) || ((option.SelectionLength == 0) && (option.SelectionStart == option.Text.Length))) && ((this.RightToLeft != RightToLeft.Yes) || ((option.SelectionLength == 0) && (option.SelectionStart == 0)))))
                    {
                        break;
                    }
                    return true;
                }
                case Keys.Down:
                    return true;

                case Keys.Delete:
                    return true;

                default:
                    base.Value = base.SearchString(keyData.ToString());
                    this.ii_TrueTextLength = this.Text.Length;
                    return false;
            }
            return !dataGridViewWantsInputKey;
        }

        public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return this.EditingControlFormattedValue;
        }

        private void OnCellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            e.Cancel = false;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            if ((this.ii_TrueTextLength != -1) && (this.Text.Length > this.ii_TrueTextLength))
            {
                base.Value = this.Text.Substring(1);
                this.ii_TrueTextLength = -1;
            }
            this.valueChanged = true;
            if (this.EditingControlDataGridView != null)
            {
                this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
                base.OnTextChanged(e);
            }
        }

        public virtual void PrepareEditingControlForEdit(bool selectAll)
        {
            this.Font = this.EditingControlDataGridView.Font;
        }

        public virtual DataGridView EditingControlDataGridView
        {
            get
            {
                return this.dataGridView;
            }
            set
            {
                this.dataGridView = value;
                this.dataGridView.CellValidating += new DataGridViewCellValidatingEventHandler(this.OnCellValidating);
            }
        }

        public virtual object EditingControlFormattedValue
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = (string) value;
            }
        }

        public virtual int EditingControlRowIndex
        {
            get
            {
                return this.rowIndex;
            }
            set
            {
                this.rowIndex = value;
            }
        }

        public virtual bool EditingControlValueChanged
        {
            get
            {
                return this.valueChanged;
            }
            set
            {
                this.valueChanged = value;
            }
        }

        public virtual Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        public virtual bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }
    }
}

